package com.pingtai.timer;

import java.util.Date;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.pingtai.ajax.entity.IndentTarget_163;
import com.pingtai.ajax.service.NoticeService;
import com.pingtai.indent.entity.IndentNumberTmp;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.user.service.UserManagerService;

public class IndentAutoCompleteTimer_taobao_sc_baobei  implements ApplicationContextAware{

	private int base;
	private  UserManagerService userManagerService;
	private  WeiboIndentService weiboIndentService;
	private  NoticeService noticeService;
	
	private  ApplicationContext context;
	
	private static boolean flag = true;
	
	public void run(){
		//if(!flag) return;
		flag = false;
		if(userManagerService==null) {
			userManagerService = (UserManagerService) context.getBean("userManagerTimerServices");
		}
			
		if(weiboIndentService==null) {
			weiboIndentService = (WeiboIndentService) context.getBean("weiboIndentTimerServices");
		}
			
		if(noticeService==null){
			noticeService = (NoticeService) context.getBean("noticeTimerServices");
		}
			
		List list = weiboIndentService.getAllRuningIndent(base);
		if(list == null || list.size()==0) {
			flag = true;
			return;
		}
		for(int m=0;m<list.size();m++){
			UserIndent userIndent = (UserIndent) list.get(m);
			String url = userIndent.getTargetUrl();
			try {
				IndentTarget_163 indentTarget = noticeService.getIndentTargetInfo_taobao_baobei(url);
				Integer nowNumber = new Integer(indentTarget.getNumber());
				Integer startNumber = userIndent.getStartNumber();
				Integer number = userIndent.getNumber();
				Integer oldNowNumber = userIndent.getNowNumber();
				if(nowNumber-startNumber>=number){
					Date d = new Date();
					userManagerService.userIndentRebate(userIndent);
					userIndent.setState(2);
					userIndent.setNowNumber(nowNumber);
					userIndent.setEndDate(d);
					weiboIndentService.updateUserIndent(userIndent);
					if(oldNowNumber!=null&&!oldNowNumber.equals(nowNumber)){
						IndentNumberTmp indentNumberTmp = new IndentNumberTmp();
						indentNumberTmp.setUserIndent(userIndent);
						indentNumberTmp.setNumber(nowNumber);
						indentNumberTmp.setDate(d);
						weiboIndentService.saveIndentNumberTmp(indentNumberTmp);
					}
				}else{
					Date d = new Date();
					userIndent.setNowNumber(nowNumber);
					weiboIndentService.updateUserIndent(userIndent);
					if(oldNowNumber!=null&&!oldNowNumber.equals(nowNumber)){
						IndentNumberTmp indentNumberTmp = new IndentNumberTmp();
						indentNumberTmp.setUserIndent(userIndent);
						indentNumberTmp.setNumber(nowNumber);
						indentNumberTmp.setDate(d);
						weiboIndentService.saveIndentNumberTmp(indentNumberTmp);
						
					}
				}
			} catch (Exception e) {
				if((m+1)==list.size()){
					flag = true;
					return;
				}
				e.printStackTrace();
				continue;
			}
		}
		flag = true;
	}

	
	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}

	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	
	public int getBase() {
		return base;
	}

	public void setBase(int base) {
		this.base = base;
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		this.context = arg0;
		
	}
}
